package com.gzdh.dishrecognition.ui.adapter.paicai;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.Bmzl;
import com.gzdh.dishrecognition.R;

import java.util.ArrayList;
import java.util.List;

public class PaiCaiRightZlAdapter extends RecyclerView.Adapter<PaiCaiRightZlAdapter.ViewHolder> {


    private final Context mContext;

    private int select = 0;

    List<Bmzl> list;

    private OnUserClick onUserClick;

    public void setOnUserClick(OnUserClick onUserClick) {
        this.onUserClick = onUserClick;
    }

    public PaiCaiRightZlAdapter(Context context) {
        this.mContext = context;
        this.list = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_str2, parent, false);
        return new ViewHolder(itemView);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        holder.nameTv.setText(list.get(position).getName());
        holder.nameTv.setMaxEms(3);
        holder.mainLinear.setOnClickListener(view -> {
            if (onUserClick != null) {
                onUserClick.onUserClick(list.get(position));
            }
        });
        holder.mainLinear.setOnClickListener(view -> {
            select = position;
            if (onUserClick != null) {
                onUserClick.onUserClick(list.get(position));
            }
            notifyDataSetChanged();
        });
        if (select == position) {
//            GradientDrawable drawable = new GradientDrawable();
//            drawable.setShape(GradientDrawable.RECTANGLE);
//            drawable.setCornerRadius(10);
//            drawable.setColor(Color.rgb(220, 220, 220));


            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);

            float[] radius = new float[8];
            radius[0] = 0;
            radius[1] = 0;
            radius[2] = 10;
            radius[3] = 10;

            radius[4] = 10;
            radius[5] = 10;
            radius[6] = 0;
            radius[7] = 0;
            drawable.setCornerRadii(radius);
            drawable.setColor(Color.rgb(200, 200, 200));
            holder.mainLinear.setBackground(drawable);
        } else {
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);
            float[] radius = new float[8];
            radius[0] = 0;
            radius[1] = 0;
            radius[2] = 10;
            radius[3] = 10;

            radius[4] = 10;
            radius[5] = 10;
            radius[6] = 0;
            radius[7] = 0;
            drawable.setCornerRadii(radius);
            drawable.setColor(Color.rgb(240, 240, 240));
            holder.mainLinear.setBackground(drawable);
        }
    }

    public void setList(List<Bmzl> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnUserClick {
        public void onUserClick(Bmzl bmzl);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;
        TextView nameTv;

        ViewHolder(View item) {
            super(item);
            mainLinear = item.findViewById(R.id.mainLinear);
            nameTv = item.findViewById(R.id.nameTv);
        }
    }

}
